package com.hdax.dm.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hdax.dm.common.api.OrderControllerApi;
import com.hdax.dm.feign.OrderFeign;
import com.hdax.dm.order.DmOrder;
import com.hdax.dm.utils.response.CommonResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/pay")
public class PayController {
    @Autowired
    private OrderFeign orderFeign;

    @SneakyThrows
    @GetMapping(path = "/zfbPay")
    public void zfbPay(@RequestParam("orderNo") String no, HttpServletResponse response){
        //支付宝支付账号  ctxnqk0865@sandbox.com
        String orderNo = no;
        List<DmOrder> orders = orderFeign.orders(orderNo);
        DmOrder order = new DmOrder();
        for(DmOrder dm:orders){
            order = dm;
        }
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2021000119635037",
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC9cfp9Gr3J1iMMVNwFKdyPHM0sEVoplTioHD0DojvCM9atIgCO3eW7d796WqK5DVAHNUcU7QJelIy8GGo7c8D22dquAfN5BDlBwu9kFr6KmueKd8LLs9A6q/4qU7V27QpArcLBSLYqeWBPrSpInJWLH5AFWk8kvMYPey8l00FCGuNbx2hDMvsUhOC4lTYHU/vnUqBJg3B+2dfF36htDN9zL5COQ7cJ5yru8ugw903TbMzbInwpeORMibNwlHjE67t0E2/FMe35wBBWXgL/2nYS4fFgkP/v67NvvlVQpC26I8iZDXM154cv+jVPjhRb+zyAGJ4e5bEeg7IS7TY/DWBTAgMBAAECggEALSBIVaKYEMsEYDyaiFRXaHOo39VmT79u1oSbeBEeAP2OJoGR9nYe8g/tOXz7g+8K4Byty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KGoKGatyuxvT4ESPSw9Jh146tTlHXO6eSXFpEQbVOeTMFiut/oOhpN8aXqt+Duup",
                "json",
                "UTF-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuJXH7ngnjMS1kpr6Hv0AVuqcVFySbgPAR72IO7VBDxrmIS8qQ1J4TE5WqDrZjrQIR+VOYjlSW+Om7xnsvgbs8yrIfz/x1Lp/n/x6C61C2dIe0/PiKd/BM89j9oVbx0DiVkH7seW+cUOS6mhBer7D8dmHp57shZZ9DTNi3xqL3ndb83o3z9wMUyB6vMiRB88TMzApo/kizW/1/qMqo2Ic4Oy/TT/cs2j8oAmB2EPAwOZ5PcvIMUm1FP5zXNE60NbSIIZpJwkmEvyOWlw3DsF5kGqoc+H46FFOLECgCOsewfluo+s26VeWa5Tmal8eqXdfpxHEEITS3kGWqf6f/fJFwQIDAQAB",
                "RSA2");
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl("http://localhost:7421/order/pay/payMsg");//支付成功后的同步回调
        alipayRequest.setNotifyUrl("http://gb6xst.natappfree.cc/order/pay/payMsg");//支付成功后的异步回调


        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = orderNo;
        //付款金额，必填
        Double total_amount = order.getTotalAmount();
        //订单名称，必填
        String subject = order.getItemName();
        //商品描述，可空
        String body = "";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //输出
        out.println(result);

    }
}
